<?php
/**
 * 新员工入职邮件提醒
 */
apf_require_class('Oa_Job_Abstract');
apf_require_class('Tools_NewGuys');
apf_require_class('Bll_Hr_OutInterface');

class Oa_Job_Flow_ToNewGuys extends Oa_Job_Abstract {
    private $read_db = "hroa_slave";
    private $write_db = "hroa_master";
    private $retry_num = 10;

    public function run() {
        $this->log("新员工入职邮件--开始 ".date('Y-m-d H:i:s'));
        /**
         * 从entry表里面查找入职时间在系统时间之后，并且mailed字段为0（表示没有发过入职邮件）的用户。
         */
        $nowtime = date('Ymd',time());
        $new_guys = array();
        $sql = "SELECT entry_id,real_name,office_mail,user_id FROM entry WHERE mailed = ? AND is_entry = ? AND entry_date >= ?"; //获取即将入职的员工
        $new_guys = $this->get_db_rows($sql, $this->read_db, array(0,1,$nowtime));

        if(empty($new_guys)) {
            exit();
        }

        $this->_send_mail($new_guys);

        $failed_mail = json_encode($new_guys);
        $this->log("新员工入职邮件--结束,没有发送成功的有".$failed_mail."   ".date('Y-m-d H:i:s'));  
    }

    private function _send_mail(&$arr) {
        for ($i=1;$i<=$this->retry_num;$i++) {
            if (!empty($arr)) {
                foreach ($arr as $k=>$val) {
                    if (Tools_NewGuys::get_instance()->check_mail($val['office_mail'])) {
                        $content = self::_build_mail_template($val['real_name']);
                        $hr_bll = new Bll_Hr_OutInterface();
                        //获得上级主管
                        $super_data = $hr_bll->get_under_leader($val['user_id']);
                        $result = Tools_NewGuys::get_instance()->send($val['office_mail'],$super_data['mail'],'欢迎加入安居客',$content);
                        if ($result == 1) {
                            $entry_id = $val['entry_id'];
                            $sql = "UPDATE entry SET mailed = 1 where entry_id=$entry_id";
                            $this->execute( $sql,array(), $this->write_db);
                            unset($arr[$k]);
                        }
                    }
                }
            } else {
                break;   
            }
        }
        return $arr;
    }

    static function _build_mail_template($user) {
        global $G_STATIC_PATH;
        $imgSrc = "$G_STATIC_PATH[0]newguys.jpg";

        $im = new Imagick($imgSrc);

        $width = $im->getImageWidth();
        $height = $im->getImageHeight();

        $N_Width = 750;
        $N_Height = (int)($height / $width * $N_Width);

        //裁剪
        $im->resizeImage($N_Width,$N_Height,Imagick::FILTER_LANCZOS, 1, true);

        $year = date('Y',time());
        $month = date('m',time());
        $day = date('d',time());

        //打字
        $draw = new ImagickDraw();
        $draw->setFont("$G_STATIC_PATH[0]msyh.ttf");
        $draw->setFontSize(30);
        $draw->setFillColor(new ImagickPixel('#60482d'));
        $im->annotateImage($draw, 140, 77, 0, $user);

        $im->setImageFormat("JPEG");
        //压缩
        $im->setImageCompression(Imagick::COMPRESSION_JPEG);
        $im->setImageCompressionQuality(80);
        $im->stripImage();
        $im->setImageColorspace(Imagick::COLORSPACE_RGB);

        $file_name = md5($user.time()).".jpg";
        
        $im->writeImage("$G_STATIC_PATH[0]newguys/$file_name");

        $im->clear();
        $im->destroy();
        unset($im);
        
        $result='<html><body><img src="'.$G_STATIC_PATH[0].'newguys/'.$file_name.'" alt="你好，欢迎你加入安居客。我们很荣幸，能在接下去的日子里与你共事。希望你能在你的岗位上发挥出你自己的特长，创造属于你自己的价值。" /></body></html>';

        return $result;
    }
}
